#!/usr/bin/python
 
###############################################################################
#
#
###############################################################################

#
# MODULES
#
import numpy,sys,os

import Nio #  for reading netCDF files

import Ngl
from read_trajectories import *


# import Scientific.IO.NetCDF # 

#---------- Get Arguments ------------------------------------------

# run number
nrun = '2'
exp = 'past'

#tcfilename = '/work/romano/fvcam2.2/3/past/all/traj_3_past'
#tcfilename = '/work/romano/fvcam2.2/3/future/all/traj_3_future'

#tcfilename = '/work/romano/fvcam2.2/3/future/A1B.h2.10-04/traj_1980'

#tcfilename = '/d/visusers/romano/python/fvcam2.2/traj_fake'
#tcfilename = '/d/visusers/romano/python/fvcam2.2/traj_fake_2'

#tcfilename = '/work/romano/fvcam2.2/all/traj_all_past'
#figname = 'tracks_all_past'

tcfilename = '/ptmp/zarzycki/output/traj_out'
figname = 'traj_out'

#if len(sys.argv) < 2:
#        print >> sys.stderr, 'Usage: %s <run #> <past|future>' % sys.argv[0]
#        print >> sys.stderr, 'in /work/romano/fvcam2.2/'
#        raise SystemExit
#else:
#        nrun = sys.argv[1]
#	exp = sys.argv[2]
#	tcfilename = '/work/romano/fvcam2.2/%s/%s/all/traj_%s_%s' % (nrun, exp, nrun, exp)
#	figname = 'tracks_%s_%s' % (nrun, exp)


#---------- Setup Plotting  ------------------------------------------

#
#  Open a workstation and specify a different color map.
#
wkres = Ngl.Resources()
wkres.wkColorMap = "default"
#wks_type = "x11"
wks_type = "pdf"
wks = Ngl.open_wks(wks_type,figname,wkres)

#---------- Get storm tracks  ------------------------------------------

[tracks] = read_trajectories(tcfilename)

# set up plot
resources = Ngl.Resources()
resources.nglFrame   = False         # don't advance frame
resources.vpWidthF   = 0.80          # make map bigger
resources.vpHeightF  = 0.80
resources.mpLimitMode = "LatLon"

resources.mpFillOn   = True
resources.mpLabelsOn = True
resources.mpOutlineOn = True
#resources.mpLandFillColor = 2
#resources.mpOceanFillColor = 3
resources.mpLandFillColor = "white"
resources.mpOceanFillColor = "white"
resources.mpGridAndLimbOn = False
resources.vpXF       = 0.05
resources.vpYF       = 0.95
resources.vpWidthF   = 0.8
resources.vpHeightF  = 0.8
resources.tmXBTickStartF = -75
resources.tmXBTickEndF = -25
resources.tmYROn  = True
resources.tmXTOn  = True
resources.tmXBLabelFontHeightF = 0.02
resources.tmYLLabelFontHeightF = 0.02
resources.mpProjection = "CylindricalEquidistant"
map = Ngl.map(wks,resources)    # Draw map.

#
#  Main title.
#
txres = Ngl.Resources()
txres.txFontHeightF = 0.025
txres.txFontColor   =  1
txres.txFont        = 22
Ngl.text_ndc(wks,"Storm Trajectories",0.52,0.90,txres)
del txres

#
#  Draw the trajectories.
#
pres = Ngl.Resources()        # polyline resources
pres.gsLineThicknessF = 3.0                # line thickness
mres  = Ngl.Resources()                        # marker resources
#mres.gsMarkerSizeF  = 17.0        # marker size
mres.gsMarkerSizeF  = .001        # marker size
mres.gsMarkerColor  = "red"     # marker color
mres.gsMarkerIndex = 16 # filled circle

# loop through trajectories
for i in range(len(tracks)):
	nt = len(tracks[i][0])
	#Ngl.polymarker(wks,cplot,[tracks[i][0]],[tracks[i][1]],resources) 
	for j in range(nt):
		x = tracks[i][0][j]
		y = tracks[i][1][j]
		#print x, y
		
		# Draw polymarkers at each point
		Ngl.polymarker(wks,map,[x],[y],mres) 

		# draw polyline
		#Ngl.polymarker(wks,cplot,[tracks[i][0][j]],[tracks[i][1][j]],resources) 

	
Ngl.frame(wks)

#---------- Clean up ------------------------------------------

# (not really necessary, but a good practice).

del map
del resources
# others...

Ngl.end()

